<?php
/*
客户跟进
*/
define('Acache_no','1');
class Con_meal_order extends Acan{
	public $defaultAct='menu';
	public $Conread = '订餐';
	public $Actread = array('list'=>'所有订餐','my'=>'我的订餐','menu'=>'菜单','save'=>'保存','del'=>'删除','delall'=>'删除所有');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_meal_order);
		$this->mmenu = Afun_db()->model(Acan_table_meal_menu);
	}
	//--所有客户
	function Act_list(){
		$this->atitle = $Actread[$this->Aact];
		return $this->Act_my();
	}
	//--自己客户
	function Act_my(){
		if(empty($this->atitle)){$this->atitle = $Actread[$this->Aact];}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$get = $_GET;
		if($get['today']==1){
			$where = " and b.date='".Afun_get_time("date")."'";
		}
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr['shopname'] = '餐厅';
		$columnrr = array_merge($columnrr,$this->model->comment);//初始栏目
		unset($columnrr[$key_name]);
		$columnrr['price'] = '价格';
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		//分页函数
		if($this->Aact=='list' && $this->uid==$uid){
			$con->where = "1".$where;
		}else if($this->Aact=='list' && $this->uid!=$uid){
			$con->where = "u.uid=$uid".$where;
		}else{
			$con->where = "u.uid=$this->uid".$where;//只能查看自己的客户
		}
		$con->where .= ' and b.id>0';
		$con->select = "b.*,c.shopname,a.name as mid,a.price,u.username as uid";
		$con->join = "LEFT JOIN `".Acan_table_meal_order."` b ON a.id=b.mid LEFT JOIN `".Acan_table_meal_shop."` c ON a.sid=c.id LEFT JOIN `".Acan_table_user."` u ON b.uid=u.uid";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = "date";
		$page->order = "DESC";
		$page->page = '20';
		$page->counts = $this->mmenu->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "c.id ASC,b.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->mmenu->findAll($con);
		include $this->Atpl($this->Amod.'/my');
	}
	//--跟进客户
	function Act_menu(){
		if(empty($this->atitle)){$this->atitle = $Actread[$this->Aact];}
		$uid = $this->uid;
		if($this->id>0){
			$con->where = 's.id='.$this->id;
		}
		$get = $_GET;
		$obj = Afun_acan_new('list');
		$key_name = $this->mmenu->key_name;
		$columnrr = $this->mmenu->comment;//初始栏目
		unset($columnrr[$key_name]);
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->mmenu,$column,'Aform');
		$column_json = $obj->form_json($this->mmenu);
		//分页函数
		$con->select = "a.*,s.shopname as sid";
		$con->join = "LEFT JOIN `".Acan_table_meal_shop."` s ON a.sid=s.id";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $key_name;
		$page->order = "DESC";
		$page->page = '20';
		$page->counts = $this->mmenu->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.sid ASC,a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->mmenu->findAll($con);
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$con->where = "a.id=".$this->id;
		$info = $this->model->ofind($con);
		Afun_json($info);
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一条记录
				$savedata['uid'] = $this->uid;
				$savedata['date'] = Afun_get_time();
				$msg = $this->model->save($savedata);
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_json($msg);
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			echo '删除成功';
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$where = " and source=".$this->uid;
		$this->model->delete($this->model->key_name." IN ($vala)".$where);
		Afun_refresh("删除成功",'referrer');
	}

}

?>